//
//  TableBarViewController.m
//  haierapp
//
//  Created by Zhou Bing on 14-8-8.
//  Copyright (c) 2014年 haier. All rights reserved.
//

#import "TableBarViewController.h"
#import "TabBarManager.h"
#import "UIColor+HexRGB.h"

#import "SMPageControl.h"

#define KEYNAME_FIRSTSETUP_USER_DEFAULTS @"firstSetup"

@interface TabBarViewController ()

@end

@implementation TabBarViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    [self.navigationController setNavigationBarHidden:YES];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] init];
    self.navigationItem.backBarButtonItem = backItem;
    
    if (__CUR_IOS_VERSION > __IPHONE_7_0) {
        backItem.title = @"";
    }else{
        backItem.title = @"返回";
    }

    // _tabBarManager = [[TabBarManager alloc] initWithTabBar:self];
    _tabBarManager = [TabBarManager shareInstance];
    [_tabBarManager setTabBarController:self];
    [self setDelegate:_tabBarManager];
    
        
    //显示引导页面
    BOOL flag=[self appIsFirstOpen];
    
    if (flag) {
        [self showIntroView];
    }
    
    //注册"打开购物车"通知事件
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(showShopCar:) name:NOTIFICATION_SHOW_SHOPCAR object:nil];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    [self.selectedViewController beginAppearanceTransition: YES animated: animated];
}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self.selectedViewController endAppearanceTransition];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [self.selectedViewController beginAppearanceTransition: NO animated: animated];
    [super viewWillDisappear:animated];
}

-(void)viewDidDisappear:(BOOL)animated
{
    [self.selectedViewController endAppearanceTransition];
    [super viewDidDisappear:animated];
}

#pragma mark - Methods
//判断是否第一次打开
-(BOOL)appIsFirstOpen{
    BOOL flag=NO;
    
    NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
    
    if ([defaults objectForKey:KEYNAME_FIRSTSETUP_USER_DEFAULTS]==nil) {
        flag=YES;
        [defaults setObject:@(NO) forKey:KEYNAME_FIRSTSETUP_USER_DEFAULTS];
    }
    
    return flag;
}

//显示购物车
-(void)showShopCar:(NSNotification *)notification{
    
    [self.navigationController popToRootViewControllerAnimated:YES];
    
    
    [self setSelectedIndex:2];
}


#pragma mark - Intro View Method
//显示介绍页面
-(void)showIntroView
{
    EAIntroPage *page1=[EAIntroPage page];
    
    page1.bgImage=[UIImage imageNamed:@"guide_bg1"];
    page1.titleIconView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"guide_text1"]];
    CGFloat positionY=self.view.frame.size.height-page1.titleIconView.frame.size.height;
    page1.titleIconPositionY=positionY;
    
    EAIntroPage *page2=[EAIntroPage page];
    page2.bgImage=[UIImage imageNamed:@"guide_bg2"];
    page2.titleIconView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"guide_text2"]];
    page2.titleIconPositionY=positionY;
    
    EAIntroPage *page3=[EAIntroPage page];
    page3.bgImage=[UIImage imageNamed:@"guide_bg3"];
    page3.titleIconView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"guide_text3"]];
    page3.titleIconPositionY=positionY;
    
    EAIntroView *intro=[[EAIntroView alloc]initWithFrame:self.view.bounds andPages:@[page1,page2,page3]];
    intro.skipButton=nil;
    
    //自定义pageControl
    SMPageControl *pageControl=[[SMPageControl alloc]init];
    pageControl.pageIndicatorImage=[UIImage imageNamed:@"guide_btn_d"];
    pageControl.currentPageIndicatorImage=[UIImage imageNamed:@"guide_btn"];
    [pageControl sizeToFit];
    intro.pageControl=(UIPageControl *)pageControl;
    intro.pageControlY=30;
    [intro setDelegate:self];
    
    [intro showInView:self.view animateDuration:0];
}

//显示自定义介绍页面
-(void)showIntroViewCustom
{
    CGFloat PointY;
    CGSize titleSize;
    CGPoint titlePoint;
    
    UIView *viewForPage1=[[UIView alloc]initWithFrame:self.view.bounds];
    UIImageView *bg1=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"guide_bg1"]];
    [viewForPage1 addSubview:bg1];
    UIImageView *title1=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"guide_text1"]];
    titleSize=title1.frame.size;
    titlePoint=title1.frame.origin;
    PointY=self.view.bounds.size.height-title1.frame.size.height;
    [title1 setFrame:CGRectMake(titlePoint.x, PointY, titleSize.width, titleSize.height)];
    [viewForPage1 addSubview:title1];
    EAIntroPage *page1=[EAIntroPage pageWithCustomView:viewForPage1];
    
    
    UIView *viewForPage2=[[UIView alloc]initWithFrame:self.view.bounds];
    UIImageView *bg2=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"guide_bg2"]];
    [viewForPage2 addSubview:bg2];
    UIImageView *title2=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"guide_text2"]];
    [title2 setFrame:CGRectMake(titlePoint.x, PointY, titleSize.width, titleSize.height)];
    [viewForPage2 addSubview:title2];
    EAIntroPage *page2=[EAIntroPage pageWithCustomView:viewForPage2];
    
    
    UIView *viewForPage3=[[UIView alloc]initWithFrame:self.view.bounds];
    UIImageView *bg3=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"guide_bg3"]];
    [viewForPage3 addSubview:bg3];
    UIImageView *title3=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"guide_text3"]];
    [title3 setFrame:CGRectMake(titlePoint.x, PointY, titleSize.width, titleSize.height)];
    [viewForPage3 addSubview:title3];
    EAIntroPage *page3=[EAIntroPage pageWithCustomView:viewForPage3];
    
    EAIntroView *intro=[[EAIntroView alloc]initWithFrame:self.view.bounds andPages:@[page1,page2,page3]];
    [intro setDelegate:self];
    intro.skipButton=nil;
    
    SMPageControl *pageControl=[[SMPageControl alloc]init];
    pageControl.pageIndicatorImage=[UIImage imageNamed:@"guide_btn_d"];
    pageControl.currentPageIndicatorImage=[UIImage imageNamed:@"guide_btn"];
    [pageControl sizeToFit];
    intro.pageControl=(UIPageControl *)pageControl;
    intro.pageControlY=30;
    
    
    [intro showInView:self.view animateDuration:0.3];
    
}


#pragma mark - EAIntroView Delegate

- (void)introDidFinish:(EAIntroView *)introView
{
    
}


@end
